package lacus.wd_cinema.repository;

import lacus.wd_cinema.entity.Screening;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Repository;

import java.time.LocalDateTime;
import java.util.List;

/**
 * 与放映相关的数据访问类
 * @author lacus
 * @version 0.3
 * @since 0.3
 */
@Repository
public interface ScreeningRepository extends JpaRepository<Screening, String> {

    @Query("SELECT s FROM Screening s " +
            "WHERE s.hall.id = :hallId " +
            "AND NOT (s.startTime > :endTime " +
            "OR s.endTime < :startTime)")
    List<Screening> conflictedScreenings(
            @Param("hallId") String hallId,
            @Param("startTime") LocalDateTime startTime,
            @Param("endTime") LocalDateTime endTime);

    boolean existsByMovieId(long movieId);

    Page<Screening> findScreeningsByMovieId(long movieId, Pageable pageable);

    Page<Screening> findScreeningsByMovieIdAndStartTimeBetween(
            long movieId, LocalDateTime start, LocalDateTime end, Pageable pageable);

    List<Screening> findAllByStartTimeBetween(
            LocalDateTime now, LocalDateTime fiveMinutesBefore);

    List<Screening> findAllByStartTimeBetweenAndStatus(
            LocalDateTime startTime, LocalDateTime startTime2, Screening.Status status);

    List<Screening> findAllByStartTimeBeforeAndEndTimeAfter(
            LocalDateTime now, LocalDateTime now1);

    List<Screening> findAllByEndTimeBefore(
            LocalDateTime now);
}
